package packVistas;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.JLabel;
import javax.swing.JTextField;

import packLigaDeFutbol.GestorTemporadas;
import packLigaDeFutbol.GestorUsuarios;
import packLigaDeFutbol.Principal;

import com.jgoodies.forms.layout.Sizes;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPasswordField;

public class UserLogin extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8901658377350811023L;
	private JTextField txtUsu;
	private JPasswordField txtPass;


	public UserLogin() {
		setModal(true);
		setResizable(false);
		setTitle("Usuarios registrados");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 369, 179);
		getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("center:min(125dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(9dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.UNRELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(14dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				new RowSpec(RowSpec.CENTER, Sizes.bounded(Sizes.DEFAULT, Sizes.constant("14dlu", false), Sizes.constant("14dlu", false)), 0),
				FormFactory.RELATED_GAP_ROWSPEC,}));
		
		JLabel lblUsuario = new JLabel("Usuario:");
		getContentPane().add(lblUsuario, "2, 2, right, default");
		
		txtUsu = new JTextField();
		txtUsu.setText("admin");
		getContentPane().add(txtUsu, "4, 2, fill, default");
		txtUsu.setColumns(10);
		
		JButton btnAceptar = new JButton("Aceptar");

		btnAceptar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				String usu = txtUsu.getText();
				String pass = new String(txtPass.getPassword());
				
				//TODO comprobar que los campos solo sean numeros y letras

				GestorUsuarios.getInstance().autenticarUsuario(usu, pass);
				switch (Principal.tipoUsu) {
				case 1:
					MenuAdmin ma = new MenuAdmin();
					ma.setVisible(true);
					dispose();
					break;
				case 2:
					MenuEquipos me = new MenuEquipos();
					me.setVisible(true);
					dispose();
					break;
				case 3:
					MenuArbitros mar = new MenuArbitros();
					mar.setVisible(true);
					dispose();
					break;
				default:
					JOptionPane.showMessageDialog(new JFrame(), 
							"El usuario o la contrase\u00F1a no son correctos.", 
							getTitle(),JOptionPane.WARNING_MESSAGE);
					break;
				}
			}
		});
		getContentPane().add(btnAceptar, "8, 2");
		
		JLabel lblNewLabel_1 = new JLabel("Password:");
		getContentPane().add(lblNewLabel_1, "2, 4, right, default");
		
		txtPass = new JPasswordField();
		txtPass.setText("admin");
		getContentPane().add(txtPass, "4, 4, fill, top");
		
		JButton btnVolver = new JButton("Volver");
		btnVolver.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();
			}
		});
		getContentPane().add(btnVolver, "8, 4");
		
		JButton btnCambiar = new JButton("Cambiar Contrase\u00F1a");
		btnCambiar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				CambiarPassword cp = new CambiarPassword();
				cp.setVisible(true);
			}
		});
		getContentPane().add(btnCambiar, "4, 6");
		
		JButton btnRecuperar = new JButton("Recuperar contrase\u00F1a");
		btnRecuperar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				RecuperarPassword rp = new RecuperarPassword();
				rp.setVisible(true);
			}
		});
		getContentPane().add(btnRecuperar, "4, 8");
	}

}
